package org.naxx.security.parser.util;

import javax.xml.stream.XMLStreamConstants;

public final class ParsingUtils {

	public static final void assertEvent(int expected, int actual) {
		if(!(expected == actual)) {
			throw new IllegalStateException("Expected '" + resolveEvent(expected)
					+ "' XML event but found '" + resolveEvent(actual) + "'");
		}
	}
	
	/**
	 * Returns string representation of the event defined in
	 * {@link XMLStreamConstants}.
	 */
	public static final String resolveEvent(int eventType) {
		switch (eventType) {
			case XMLStreamConstants.ATTRIBUTE:
				return "ATTRIBUTE";
			case XMLStreamConstants.CDATA:
				return "CDATA";
			case XMLStreamConstants.CHARACTERS:
				return "CHARACTERS";
			case XMLStreamConstants.COMMENT:
				return "COMMENT";
			case XMLStreamConstants.DTD:
				return "DTD";
			case XMLStreamConstants.END_DOCUMENT:
				return "END_DOCUMENT";
			case XMLStreamConstants.END_ELEMENT:
				return "END_ELEMENT";
			case XMLStreamConstants.ENTITY_DECLARATION:
				return "ENTITY_DECLARATION";
			case XMLStreamConstants.ENTITY_REFERENCE:
				return "ENTITY_REFERENCE";
			case XMLStreamConstants.NAMESPACE:
				return "NAMESPACE";
			case XMLStreamConstants.NOTATION_DECLARATION:
				return "NOTATION_DECLARATION";
			case XMLStreamConstants.PROCESSING_INSTRUCTION:
				return "PROCESSING_INSTRUCTION";
			case XMLStreamConstants.SPACE:
				return "SPACE";
			case XMLStreamConstants.START_DOCUMENT:
				return "START_DOCUMENT";
			case XMLStreamConstants.START_ELEMENT:
				return "START_ELEMENT";
			default:
				throw new IllegalStateException("Unmatched XML event '" 
						+ eventType + "'");
		}
	}
	
	private ParsingUtils() {
		throw new IllegalStateException("Can not instantiate class '" +
				ParsingUtils.class.getCanonicalName() + "'");
	}
}
